#include "data_bean.h"


// User Methods
User::User(QString id, QString password, USER_ROLE role, QString email = "", QString phone = "")
{
    this->id = id;
    this->password = password;
    this->role = role;
    this->email = email;
    this->phone = phone;
}

QString User::getPassword() const
{
    return password;
}

void User::setPassword(const QString &value)
{
    password = value;
}

QString User::getEmail() const
{
    return email;
}

void User::setEmail(const QString &value)
{
    email = value;
}

QString User::getPhone() const
{
    return phone;
}

void User::setPhone(const QString &value)
{
    phone = value;
}

USER_ROLE User::getRole() const
{
    return role;
}

void User::setRole(const USER_ROLE &value)
{
    role = value;
}

QString User::getId() const
{
    return id;
}

void User::setId(const QString &value)
{
    id = value;
}

bool User::operator== (const User& user) {
    return this->id == user.id;
}

bool User::operator== (const QString& str) {
    return this->id == str;
}

QJsonObject User::to_json()
{
    QJsonObject obj;

    QString role_str;
    if(role == MANAGER) {
        role_str = "manager";
    }
    else if(role == TEACHER) {
        role_str = "teacher";
    }
    else {
        role_str = "student";
    }

    obj.insert("id", this->id);
    obj.insert("password", password);
    obj.insert("email", email);
    obj.insert("phone", phone);
    obj.insert("role", role_str);

    return obj;
}


// Group Methods
Group::Group(QString gid, QString gname, QString mgr)
{
    this->group_id = gid;
    this->group_name = gname;
    this->group_manager = mgr;
}

QString Group::getGroup_name() const
{
    return group_name;
}

void Group::setGroup_name(const QString &value)
{
    group_name = value;
}

QString Group::getGroup_manager() const
{
    return group_manager;
}

void Group::setGroup_manager(const QString &value)
{
    group_manager = value;
}

bool Group::operator== (const Group& group) {
    return this->group_id == group.group_id;
}

bool Group::operator== (const QString& str) {
    return this->group_id == str;
}

QString Group::getGroup_id() const
{
    return group_id;
}

void Group::add_user(const QString &uid)
{
    members.push_back(uid);
}

bool Group::remove_user(const QString &uid)
{
    if(members.indexOf(uid) == -1) {
        return false;
    }
    members.remove(members.indexOf(uid));
    return true;
}

QVector<QString>& Group::getMembers()
{
    return members;
}

int Group::get_member_size()
{
    return members.size();
}

QJsonObject Group::to_json()
{
    QJsonObject obj;
    obj.insert("gid", group_id);
    obj.insert("gname", group_name);
    obj.insert("manager", group_manager);

    QJsonArray array;
    for(auto member : members) {
        array.append(member);
    }
    obj.insert("members", array);

    return obj;
}


// Task Methods
Task::Task(QString tid, QString tname, QString owner, QDateTime begin_time, QDateTime end_time, QString save_path, TASK_STATE state)
{
    this->task_id = tid;
    this->task_name = tname;
    this->task_owner = owner;
    this->begin_time = begin_time;
    this->end_time = end_time;
    this->save_path = save_path;
    this->task_state = state;
}

QString Task::getTask_name() const
{
    return task_name;
}

void Task::setTask_name(const QString &value)
{
    task_name = value;
}

QDateTime Task::getEnd_time() const
{
    return end_time;
}

void Task::setEnd_time(const QDateTime &value)
{
    end_time = value;
}

TASK_STATE Task::getTask_state() const
{
    return task_state;
}

void Task::setTask_state(const TASK_STATE &value)
{
    task_state = value;
}

QString Task::getSave_path() const
{
    return save_path;
}

void Task::setSave_path(const QString &value)
{
    save_path = value;
}

bool Task::operator== (const Task& task) {
    return this->task_id == task.task_id;
}

bool Task::operator== (const QString &str)
{
    return this->task_id == str;
}

void Task::add_member(const QString &member, const QString& file_name = "")
{
    member_hand_in_state_map.insert(member, file_name);
}

void Task::add_member(Group &group) {
    QVector<QString> members = group.getMembers();

    for(int i=0; i < members.size(); i++) {
        member_hand_in_state_map.insert(members[i], "");
    }
}

QString Task::getTask_owner() const
{
    return task_owner;
}

QMap<QString, QString>& Task::getMember_hand_in_state_map()
{
    return member_hand_in_state_map;
}

QJsonObject Task::to_json()
{
    QJsonObject obj;
    QString state_str;

    if(task_state == ACTIVE) {
        state_str = "active";
    }
    else {
        state_str = "completed";
    }

    obj.insert("tid", task_id);
    obj.insert("tname", task_name);
    obj.insert("begin_time", begin_time.toString("yyyy-MM-dd hh:mm:ss"));
    obj.insert("end_time", end_time.toString("yyyy-MM-dd hh:mm:ss"));
    obj.insert("owner", task_owner);
    obj.insert("state", state_str);
    obj.insert("save_path", save_path);

    QJsonObject hand_in;
    for(auto key : member_hand_in_state_map.keys()) {
        hand_in.insert(key, member_hand_in_state_map[key]);
    }
    obj.insert("hand_in_states", hand_in);

    return obj;
}
